package com.alibaba.springboot.health.pojo;

import java.util.List;

public class Page<T> {
    //已知数据
    private int currentPage;//当前页   通过前台传值
    private int pageSize;//每页显示的数据条数
    private int dataTotal;//总的记录条数，通过数据库查出

    private int totalPage;//总页数

    private int startIndex;//开始索引,用来在数据库用limit查询的时候的起始位置

    private List<T> list;//当前页的数据

    //分页显示的页数
    private int start;
    private int end;

    //初始化数据
    public Page(int currentPage, int pageSize, int dataTotal) {
        super();
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.dataTotal = dataTotal;

        //如果数据总数跟每页条数取余为0  表示每页正好显示pageSize条数据
        if (dataTotal % pageSize == 0) {
            this.totalPage = dataTotal / pageSize;
        } else {
            //如果不整除，后面加一来显示多余的数据
            this.totalPage = dataTotal / pageSize + 1;
        }

        //开始索引
        this.startIndex = (currentPage - 1) * pageSize;

        this.start = 1;
        this.end = 5;

        if (totalPage <= 5) {
            //如果总页数小于5，那么end就为总页数的值
            this.end = totalPage;
        } else {
            //否则，根据当前页来判断start和end为多少
            if (start < 3) {
                this.start = 1;
                this.end = 5;
            } else {
                this.start = currentPage - 2;
                this.end = currentPage + 2;
            }

        }

        if (end > totalPage) {
            this.end = totalPage;
            this.start = end - 5;
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getDataTotal() {
        return dataTotal;
    }

    public void setDataTotal(int dataTotal) {
        this.dataTotal = dataTotal;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

}



